import 'package:enterprise_v6_app/app/components/me_regional_organization_tree/regional_organization_tree_controller.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_risk_object_by_id_entity.dart';

/// 区域组织架构树状(我的任务模块)
class MeRegionalOrganizationTreePage extends StatelessWidget {
  MeRegionalOrganizationTreePage({required this.id, super.key});
  final String id;
  final controller = Get.put(MeRegionalOrganizationTreeController());

  /// 部门数据每一项的显示
  /// [treeEntity] 当前节点数据
  /// [level] 所在层级
  Widget _getTreeView(GetRiskObjectByIdObjectChildren treeEntity, int level) {
    final bool isSelected = treeEntity.id == controller.selectedEntity.id;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          constraints: BoxConstraints(
            minHeight: 55.w,
          ),
          // padding: EdgeInsets.symmetric(vertical: 10.w),
          child: Row(
            children: [
              // 间距
              SizedBox(width: 15.w * level),
              Expanded(
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 6.w),
                  decoration: BoxDecoration(
                    color: isSelected ? const Color(0xff5C6DFF) : const Color(0xffffffff),
                    borderRadius: BorderRadius.circular(4.w),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        treeEntity.name!,
                        style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                          color: isSelected ? const Color(0xffffffff) : const Color(0xff34406D),
                        ),
                      ),
                      treeEntity.objectChildren!.isNotEmpty
                          ? SizedBox(
                              width: 18.w,
                              child: SvgPicture.asset(
                                AppImages.icon_up_dowm_arrow,
                                width: 16.w,
                                height: 16.w,
                                colorFilter: const ColorFilter.mode(Colors.blue, BlendMode.srcIn),
                              ))
                          : SizedBox(
                              width: 18.w,
                            ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ).clickOnce(() {
          controller.clickOnNode(treeEntity);
        }),
        // Divider(
        //   height: 0.1.w,
        //   color: const Color(0xffDDE6F0),
        // ),
        AnimatedSize(
          duration: const Duration(milliseconds: 700),
          // curve: Curves.easeInOut,
          curve: Curves.easeInOutExpo,
          child: SizedBox(
            height: treeEntity.isExpand! ? null : 0,
            child: Column(
              children: [
                ...treeEntity.objectChildren!.map((item) {
                  return Container(
                    child: _getTreeView(item, level + 1),
                  );
                }),
              ],
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        initState: (state) {
          controller.onInitData(id);
        },
        init: controller,
        builder: (_) {
          return Container(
            padding: EdgeInsets.symmetric(horizontal: 10.w),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4.w),
            ),
            child: Column(
              children: controller.treeList.map((item) {
                return _getTreeView(item, 0);
              }).toList(),
            ),
          );
        });
  }
}
